<template>
	<view class="mainpadding">
		<view class="mainpadding ffffff radius flexbetween">
			<view class="flexleft">
				<view class="titletext xiaohei sanshier fonweight margin_right2">评分</view>
				<u-rate :count="count" v-model="score" activeColor="#f8b900"></u-rate>
			</view>
			<view class="xiaohei ershiba" v-if="score==5">好评</view>
			<view class=" xiaohei ershiba" v-if="score<=4 && score>2">中评</view>
			<view class=" xiaohei ershiba" v-if="score<=2 && score!=0">差评</view>
		</view>
		<view class="mainpadding_top ffffff radius">
			<textarea placeholder="写评论~" style="width: 100%;height: 200rpx;" v-model="content"></textarea>
			<view class="flexleft flex_wrap">
				<view class="shilitu margin_top margin_right2 dingwei" v-for="(item,index) in images_text"
					:key="index">
					<image :src="item" mode=""></image>
					<view class="shanchu" @click="delindex(index,images_text,images)">
						<u-icon name="close-circle-fill"></u-icon>
					</view>
				</view>
				<view class="margin_top shangc margin_right2" v-if="images_text.length<5"
					@click="uploadImg(5,images,images_text)">
					<image src="../static/image/system/shangchuan.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="gudingdb mainpadding">
			<view class="bigbtn" @click="submit()">提交评价</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				images: [],
				images_text: [],
				score:0,
				type:1,//1商品评价,2课程评价
				listid:"",
				content:"",
			}
		},
		onLoad(options) {
			this.listid = options.id
			this.type = options.type
			this.getDetail()
		},
		methods: {
			getDetail(){
				let data = {
					id:this.listid
				}
				let url = "/api/scoregoods/myScoreGoodsCommentDetail"
				if(this.type==2){
					url = "/api/kccl/myKcCommentDetail"
				}
				if(this.type==3){
					url = "/api/cgcl/myCgCommentDetail"
				}
				httpRequest.request(url, 'GET', data, false, false, true).then(res => {
					this.content = res.data.content
					this.score = res.data.score
					this.images_text = res.data.images_text
					if(res.data.images){
						this.images = res.data.images.split(',')
					}
					
				})
			},
			submit(){
				if(this.score==0){
					httpRequest.toast("请打分后提交！")
					return false
				}
				if(this.content==""){
					httpRequest.toast("请输入评价内容！")
					return false
				}
				let state = 0
				if(this.score<=2 && this.score!=0){
					state = 2
				}
				if(this.score<=4 && this.score>2){
					state = 1
				}
				if(this.score==5){
					state = 0
				}let data = {
					id:this.listid,
					content:this.content,
					images:this.images.join(),
					score:this.score,
					state:state
				}
				let url = "/api/scoregoods/editScoreGoodsCommentDetail"
				if(this.type==2){
					url = "/api/kccl/editkcCommentDetail"
				}
				if(this.type==3){
					url = "/api/cgcl/editCgCommentDetail"
				}
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				httpRequest.request(url, 'POST', data, false, false, true).then(res => {
					uni.hideLoading()
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				})
				
			},
			uploadImg(value, upImgs, upImgs_text) { //上传图片
				let _this = this;
				let num =  Number(value) - upImgs.length;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			delindex(index, upImg, upImgs_text) {
				upImg.splice(index, 1)
				upImgs_text.splice(index, 1)
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #f9f9f9;
	}
	.shangc {
		width: 160rpx;
		height: 160rpx;
	
		image {
			width: 100%;
			height: 100%;
		}
	}
	.shanchu {
		position: absolute;
		top: -10rpx;
		right: -10rpx;
	}
	
	.shanchu2 {
		position: absolute;
		bottom: -10rpx;
		right: -50%;
		transform: translate(0, -50%);
	}
	
	.shilitu {
		width: 160rpx;
		height: 160rpx;
	
		image {
			width: 100%;
			height: 100%;
		}
	}
</style>
